package com.shigu.admin.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.account.bo.AddUserVoucherRequest;
import com.shigu.account.bo.RewardUserRequest;
import com.shigu.account.exception.UtilsAccountException;
import com.shigu.account.service.UserAccountService;
import com.shigu.account.service.UserService;
import com.shigu.account.vo.CashRecordVo;
import com.shigu.account.vo.MallUserVo;
import com.shigu.account.vo.MoneyRecordVo;
import com.shigu.account.vo.WaitCashVo;
import com.shigu.component.enums.SessionEnum;
import com.shigu.component.shiro.AuthorityUser;
import com.shigu.exceptions.AdminException;
import com.shigu.user.bo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/*
 * create by  过儿  2019-05-08
 *
 */
@Controller
@RequestMapping("adminjson")
public class CashController {

    @Autowired
    UserService userService;
    @Autowired
    UserAccountService userAccountService;

    @RequestMapping("rewardUser")
    @ResponseBody
    public MallResponse rewardUser(RewardUserRequest request) throws UtilsAccountException {
        userAccountService.rewardUser(Long.valueOf(request.getUserId()),request.getMoney());
        return new MallResponse();
    }

    /**
     * @description 增加用户的抵用券
     * @author 郭尔
     * @date 2019-11-12 15:57
     */
    @RequestMapping("addUserVoucher")
    @ResponseBody
    public MallResponse addUserVoucher(AddUserVoucherRequest request) throws UtilsAccountException {
        userService.addUserVoucher(Long.valueOf(request.getUserId()),request.getVoucher());
        return new MallResponse();
    }


    @RequestMapping("mallUserList")
    @ResponseBody
    public PageResponse<MallUserVo> mallUserList(MallUserListRequest request){
        return userService.mallUserList(request.getPage(),request.getSize(),request.getUserId(),
                                          request.getTelephone(),request.getNick(),request.getWxNick(),
                                          request.getUserType(), request.getVipLevel(),
                                          request.getAgentId(),request.getSortType(),
                                          request.getSortOrder());
    }

    @RequestMapping("moneyRecord")
    @ResponseBody
    public PageResponse<MoneyRecordVo> moneyRecord(@Valid MoneyRecordRequest request, BindingResult result) throws AdminException {
        if(result.hasErrors()){
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return userService.moneyRecord(request.getPage(),request.getSize(),request.getUserId());
    }

    @RequestMapping("waitCashList")
    @ResponseBody
    public PageResponse<WaitCashVo> waitCashList( WaitCahRequest request) {
        return userService.waitCashList(request.getPage(),request.getSize(),request.getUserId());
    }

    @RequestMapping("examineCash")
    @ResponseBody
    public synchronized MallResponse examineCash(@Valid ExamineCashRequest request, BindingResult result, HttpSession session) throws Exception {
        if(result.hasErrors()){
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        AuthorityUser user = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        return userService.examineCash(request.getApplyId(),request.getPass(),request.getReason(),user.getUserId());
    }

    @RequestMapping("cashRecord")
    @ResponseBody
    public PageResponse<CashRecordVo> cashRecord(CashRecrodRequest request){
        return userService.cashRecord(request.getPage(),request.getSize(),request.getUserId(),request.getStatus());
    }


}
